#include <nice2d/tilemap.h>
#include <nice2d/piece/piece.h>
#include <nice2d/piece/actions/piecemovesequenceaction.h>
#include <nice2d/piece/actions/piecemoveaction.h>

IMPLEMENT_OBJECT(PieceMoveSequenceAction,SequenceAction)

class PieceMoveSequenceActionData
{
public:
    std::shared_ptr<PieceScene> pieceScene;
};

PieceMoveSequenceAction::PieceMoveSequenceAction(std::shared_ptr<PieceScene> scene):
    data(new PieceMoveSequenceActionData)
{
    data->pieceScene = scene;
}

PieceMoveSequenceAction::~PieceMoveSequenceAction()
{
}

void PieceMoveSequenceAction::setMoveSequence(const std::vector<sf::Vector2i> &sequences, float duration)
{
    assert(!getOwner().expired());

    auto piece = getOwner().lock()->as<Piece>();
    assert(piece);

    auto tile = piece->getConfiguratedTile();
    assert(!tile.expired());
    auto tilePosition = tile.lock()->getTileIndex();

    auto itr = sequences.begin();
    while(itr != sequences.end())
    {
        auto action = std::make_shared<PieceMoveAction>(data->pieceScene,tilePosition,duration);
        action->setOwner(piece);
        action->setTargetTile({itr->x,itr->y});
        auto tile = data->pieceScene->getTileMap()->getTileByIndex(itr->x,itr->y);
        addAction(action);

        tilePosition = {itr->x,itr->y};
        itr ++;
    }
}
